package com.frxuexi.department.repository

import com.frxuexi.department.ApiService
import com.frxuexi.department.entity.DepartmentEntity
import com.frxuexi.lib_base.ApiResponse
import com.frxuexi.lib_base.BaseRepository
import com.frxuexi.lib_http.HttpType
import com.frxuexi.lib_http.NetWorkFactory
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import javax.inject.Inject

@OptIn
class DepartRepository @Inject constructor():BaseRepository() {
    val apiService by lazy { NetWorkFactory.factory(HttpType.NONE).create(ApiService::class.java) }
    fun getDepartments(map:Map<String,Int>): Flow<ApiResponse<List<DepartmentEntity>>>{
        return apiService.getDepartments(mapToBody(map))
    }

    fun deleteDoc(map:Map<String,Int>):Flow<ApiResponse<List<DepartmentEntity>>>{
        return apiService.deleteDoc(mapToBody(map))
    }

    fun uploadDoc(name:String,path:String):Flow<ApiResponse<List<DepartmentEntity>>>{
        val body =
            RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file = File(path))
        val multipartBody = MultipartBody.Builder().addFormDataPart("file", name, body).build()
        return apiService.uploadDoc(multipartBody.parts)
    }

    fun addDoc(map:Map<String,Any>):Flow<ApiResponse<List<DepartmentEntity>>>{
        return apiService.addDoc(mapToBody(map))
    }
}